package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.SystemService;
import com.itheima.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Description 系统相关的控制器
 * @Author makun
 * @Date 2023/9/26 19:42
 * @Version 1.0
 */
@RestController
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private SystemService systemService;

    /**
     * 根据权限获取用户的菜单
     * @return
     */
    @GetMapping("/userMenus")
    public Result userMenu(HttpServletRequest request) {
        return Result.buildSuccess(systemService.getMenusByUserId(Integer.parseInt(request.getAttribute("userId").toString())));
    }

    /**
     * 判断token是否有效
     * @param token
     * @return
     */
    @GetMapping("/isToken")
    public Result isTokenEffective(String token) {
        Claims claims = JWTUtil.checkJWT(token);
        return Objects.isNull(claims) ? Result.buildFail() : Result.buildSuccess();
    }
}
